package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ExceptionUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.common.utils.html.EscapeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TMajorPlanMapper;
import com.ruoyi.system.domain.TMajorPlan;
import com.ruoyi.system.service.ITMajorPlanService;
import com.ruoyi.common.core.text.Convert;

import javax.validation.ConstraintViolationException;
import javax.validation.Validator;

/**
 * 专业考试时间计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-19
 */
@Service
public class TMajorPlanServiceImpl implements ITMajorPlanService 
{
    @Autowired
    private TMajorPlanMapper tMajorPlanMapper;
    @Autowired
    protected Validator validator;

    private static final Logger log = LoggerFactory.getLogger(TMajorPlanServiceImpl.class);
    /**
     * 查询专业考试时间计划
     * 
     * @param majorPlanId 专业考试时间计划主键
     * @return 专业考试时间计划
     */
    @Override
    public TMajorPlan selectTMajorPlanByMajorPlanId(Long majorPlanId)
    {
        return tMajorPlanMapper.selectTMajorPlanByMajorPlanId(majorPlanId);
    }

    /**
     * 查询专业考试时间计划列表
     * 
     * @param tMajorPlan 专业考试时间计划
     * @return 专业考试时间计划
     */
    @Override
    public List<TMajorPlan> selectTMajorPlanList(TMajorPlan tMajorPlan)
    {
        return tMajorPlanMapper.selectTMajorPlanList(tMajorPlan);
    }

    /**
     * 新增专业考试时间计划
     * 
     * @param tMajorPlan 专业考试时间计划
     * @return 结果
     */
    @Override
    public int insertTMajorPlan(TMajorPlan tMajorPlan)
    {
        tMajorPlan.setCreateTime(DateUtils.getNowDate());
        return tMajorPlanMapper.insertTMajorPlan(tMajorPlan);
    }

    /**
     * 修改专业考试时间计划
     * 
     * @param tMajorPlan 专业考试时间计划
     * @return 结果
     */
    @Override
    public int updateTMajorPlan(TMajorPlan tMajorPlan)
    {
        tMajorPlan.setUpdateTime(DateUtils.getNowDate());
        return tMajorPlanMapper.updateTMajorPlan(tMajorPlan);
    }

    /**
     * 批量删除专业考试时间计划
     * 
     * @param majorPlanIds 需要删除的专业考试时间计划主键
     * @return 结果
     */
    @Override
    public int deleteTMajorPlanByMajorPlanIds(String majorPlanIds)
    {
        return tMajorPlanMapper.deleteTMajorPlanByMajorPlanIds(Convert.toStrArray(majorPlanIds));
    }

    /**
     * 删除专业考试时间计划信息
     * 
     * @param majorPlanId 专业考试时间计划主键
     * @return 结果
     */
    @Override
    public int deleteTMajorPlanByMajorPlanId(Long majorPlanId)
    {
        return tMajorPlanMapper.deleteTMajorPlanByMajorPlanId(majorPlanId);
    }

    @Override
    public String importData(List<TMajorPlan> userList, Boolean isUpdateSupport, String operName) {
        {
            if (StringUtils.isNull(userList) || userList.isEmpty()) {
                throw new ServiceException("导入用户数据不能为空！");
            }
            int successNum = 0;
            int failureNum = 0;
            StringBuilder successMsg = new StringBuilder();
            StringBuilder failureMsg = new StringBuilder();
            for (TMajorPlan user : userList) {
                try {
                    // 验证是否存在这个基础1id
                    if (user.getMajorPlanId() == null) {
                        BeanValidators.validateWithException(validator, user);
                        user.setUpdateBy(operName);
                        user.setUpdateTime(DateUtils.getNowDate());
                        user.setCreateBy(operName);
                        user.setCreateTime(DateUtils.getNowDate());
                        tMajorPlanMapper.insertTMajorPlan(user);
                        successNum++;
                        successMsg.append("<br/>" + successNum + "、新课程名称 " + user.getNewSubjectName() + " 旧课程名称 " + user.getOldSubjectName() + " 导入成功");
                    } else if (isUpdateSupport) {
                        BeanValidators.validateWithException(validator, user);
                        user.setUpdateBy(operName);
                        user.setUpdateTime(DateUtils.getNowDate());
                        tMajorPlanMapper.updateTMajorPlan(user);
                        successNum++;
                        successMsg.append("<br/>" + successNum + "、新课程名称 " + user.getNewSubjectName() + " 旧课程名称 " + user.getOldSubjectName() + " 更新成功");
                    } else {
                        failureNum++;
                        failureMsg.append("<br/>" + failureNum + "、新课程名称 " + user.getNewSubjectName() + " 旧课程名称 " + user.getOldSubjectName() + " 已存在");
                    }
                } catch (Exception e) {
                    failureNum++;
                    String loginName = "";
                    if (ExceptionUtil.isCausedBy(e, ConstraintViolationException.class)) {
                        loginName = EscapeUtil.clean(loginName);
                    }
                    String msg = "<br/>" + failureNum + "、账号 " + loginName + " 导入失败：";
                    failureMsg.append(msg + e.getMessage());
                    log.error(msg, e);
                }
            }
            if (failureNum > 0) {
                failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                throw new ServiceException(failureMsg.toString());
            } else {
                successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
            }
            return successMsg.toString();
        }
    }
}
